App.controller("productClassUserCtrl",function($scope, $rootScope, $location, $http, $compile, $stateParams, $timeout, path, publicService) {
	
	$scope.form = {};
	var classData = [];		//分类数据
	
	var alertModel = {};	//alert模态框

	//提示信息
	$scope.alertMessage = function(message){
		alertModel.pannel = jqueryAlert({
			content: message,
			modal: true
		});
	}
	
	//获取分类数据
	$scope.getClassData = function(){ 
		$http({
			   url:	path+'/server/querybrandclassifyentityall',
			   method: 'GET'   
		}).success(function(data){
			if(data.code==0){  //成功
				classData = data.result;
				$scope.showData(classData);
			}else if(data.code==1){  //失败
				
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//显示数据
	$scope.showData = function(classData){ 
		var html = '';
		
		$.each(classData, function(index, OneObj){
			var id = OneObj.id;
			var p_id = OneObj.p_id;
			
			if(!OneObj.see){
				html += ('<div class="form-horizontal" id="parent_'+id+'">'
							+'<div class="form-group">'
								+'<div class="col-sm-1 textRight">'
									+'<i class="fa fa-chevron-right" ng-click="clickParent('+id+')"></i>'
								+'</div>'
								+'<div class="col-sm-8 parent">'+OneObj.name+'</div>'
								+'<div class="col-sm-3 textRight">'
									+'<i class="fa fa-plus-square" ng-click="addClass('+id+')"></i>'
									+'<i class="fa fa-pencil-square-o" ng-click="editClass('+id+',\''+OneObj.name+'\')"></i>'
									+'<i class="fa fa-times" ng-click="deleteClass('+id+')"></i>'
								+'</div>'
							+'</div>'
						+'</div>');
			}
			if(!p_id){
				html += '<div style="display:none;">';
				$.each(classData, function(index_2, OneObj_2){
					if(OneObj_2.p_id==id){
						html += ('<div class="form-horizontal">'
									+'<div class="form-group">'
										+'<div class="col-sm-1 textRight"></div>'
										+'<div class="col-sm-8 son">'+OneObj_2.name+'</div>'
										+'<div class="col-sm-3 textRight">'
											+'<i class="fa fa-pencil-square-o" ng-click="editClass('+id+',\''+OneObj_2.name+'\')"></i>'
											+'<i class="fa fa-times" ng-click="deleteClass('+id+')"></i>'
										+'</div>'
									+'</div>'
								+'</div>');	
						OneObj_2.see = true;
					}
				});
				html += '</div>';
			}
		});
		
		html = $compile(html)($scope);   //angularJs代码需要动态编译
		$("#tree").html(html);
	}
	
	//添加分类
	$scope.addClass = function(parent_id){ 
		$scope.form = {};
		$scope.form.p_id = parent_id;
		$scope.classWindow.center().open();   //打开弹框
	}
	
	//创建分类
	$scope.createClass = function(parent_id){ 
		if(!$scope.form.name){
			$scope.alertMessage("请输入【分类名称】！");
			return;
		}
		
		var url = '';
		if(!$scope.form.id){
			url = path+'/server/addbrandclassify';
		}else{
			url = path+'/server/modifybrandclassify';
		}
		
		$http({
			   url: url,
			   method: 'POST',
			   data: angular.toJson($scope.form),  
		}).success(function(data){
			if(data.code==0){
				$scope.classWindow.close();
				$scope.alertMessage("成功！");
				$scope.getClassData();	
			}else{
				$scope.alertMessage("失败！");
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//编辑分类
	$scope.editClass = function(id,name){ 
		$scope.form = {};
		$scope.form.id = id;
		$scope.form.name = name;
		$scope.classWindow.center().open();   //打开弹框
	}
	
	//删除分类
	$scope.deleteClass = function(id){ 
		alertModel.confirm = jqueryAlert({
			content: "确定？",
			modal: true,
			width: "300",
			"buttons" :{
				"确定" : function(){
					alertModel.confirm.close();

					$http({
						   url: path+'/server/removebrandclassify/'+id,
						   method: 'GET' 
					}).success(function(data){
						if(data.code==0){
							$scope.alertMessage("成功！");
							$scope.getClassData();	
						}else{
							$scope.alertMessage("失败！");
						}
					}).error(function(data){
						alert(data);   
					})
				},
				"取消" : function(){
					alertModel.confirm.close();
				}
			}
		});
	}
	
	//点击父类
	$scope.clickParent = function(id){ 
		var chooseObj = $("#parent_"+id).children("div").children("div:nth(0)").children("i");
		var className = chooseObj.attr("class");
		if(className=="fa fa-chevron-right"){
			chooseObj.attr("class","fa fa-chevron-down");
		}else if(className=="fa fa-chevron-down"){
			chooseObj.attr("class","fa fa-chevron-right");
		}
		
		var sonObj = $("#parent_"+id).next();
		$(sonObj).slideToggle("slow");
	}
	

	$scope.getClassData();		//获取分类数据
	
	
});